package com.chasing.baseframe.db;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.chasing.baseframe.db.dao.FileDownloadCacheDao;
import com.chasing.baseframe.db.entity.FileDownloadCacheEntity;

@Database(version = 1, exportSchema = false, entities = {
        FileDownloadCacheEntity.class
})
public abstract class MyDatabase extends RoomDatabase {

    public abstract FileDownloadCacheDao fileDownloadCacheDao();

    private static final String DB_NAME = "my_database.db";
    private static volatile MyDatabase INSTANCE;

    public static MyDatabase getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (MyDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                    MyDatabase.class, DB_NAME)
                            .addMigrations(new Migration1To2())
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    private static class Migration1To2 extends Migration {
        public Migration1To2() {
            super(1, 2);
        }

        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            // 数据库的升级语句，注意：int类型对应INTEGER默认是not null的，String类型则是对应TEXT
        }
    }
}
